<#include "../common/com.html"/> <@layout>

<!-- ace styles -->
<link rel="stylesheet" href="${ctx}/static/assets/css/chosen.css" />
<link href='${ctx}/static/assets/css/select2.css' media='all' rel='stylesheet' type='text/css'/>
<script src='${ctx}/static/assets/js/select2.min.js' type='text/javascript'></script>
<script src='${ctx}/static/assets/js/select2_locale_zh-CN.js' type='text/javascript'></script>
<link rel="stylesheet" href="${ctx}/static/assets/css/jquery-ui-1.10.3.custom.min.css" />
<link href='${ctx}/static/easyui/1.3.2/themes/bootstrap/easyui.css'
      type='text/css' rel='stylesheet'>
<link href='${ctx}/static/easyui/1.3.2/themes/icon.css' type='text/css' rel='stylesheet'>


<script type="text/javascript">
function addthis(){
   var url = '${ctx}/xskd/spxskd';
    var xfbz=document.getElementsByName("xfbz");
    if(xfbz.length>0){
    if(confirm("确定要提交吗？")){
        $('#form1').form('submit', {
            url : url,
            onSubmit : function() {
                return $(this).form('validate');
            },
            success : function(data) {
                if(data!="0"){
                    alert("挂单成功！");
                    window.location.href = '${ctx}/xskd?card='+data;
                }     else{
                    alert("挂单失败！");
                }
            }
        });
    }
    }   else {
        alert("没有录入消费信息！");
    }
}

function jzthis(){
	$("#xfsjjga").attr("value",$("#hj").text());
    if($("#vipcard").val()=="" && $("#moneys").val() ==""){
        alert("卡消费现金不能都为空！");
        return;
    }
	   var url = '${ctx}/xskd/spxskdjz';
	    var xfbz=document.getElementsByName("xfbz");
	    if(xfbz.length>0){
	    if(confirm("确定要提交吗？")){
	        $('#form1').form('submit', {
	            url : url,
	            onSubmit : function() {
	                return $(this).form('validate');
	            },
	            success : function(data) {
	                if(data!="0"){
	                    alert("结账成功！");
	                    window.location.href = ' ${ctx}/xskd?card='+data;
	                }     else{
	                    alert("结账失败！");
	                }
	            }
	        });
	    }
	    }   else {
	        alert("没有录入消费信息！");
	    }
	}
</script>
<body>
<div class="page-content" style="overflow: hidden">
    <div class="row-fluid">
        <div class="span12 ">
            <form  id='form1'>
                <input type="hidden" name="card" id="card" value="${(card)!}">
               <input type="hidden" name="xfsjjga" id="xfsjjga">
               <#list  newlist as m>
             <input type="hidden" name="ye" id="ye" value="${(m.ye)!}" >
              </#list>
            <table id="tabnew" class="table  table-bordered table-condensed ">
                <thead>
                <tr>
                    <th>名称</th>
                    <th>数量</th>
                    <th>价格</th>
                    <th>折扣</th>
                    <th>实际价格</th>
                    <th>提成</th>
                    <th>操作人员</th>
                </tr>
                </thead>
                <tbody>
                <tr class="info" id="tr0">
                </tr>
                </tbody>
            </table>

                <div class="hr hr8 hr-double hr-dotted"></div>
            <div class="row-fluid">
                <div class="span6 pull-left">
                    <h4 class="pull-left">
                        累计数量: <span class="red" id="ljsl">0</span> 合计: <span class="red" id="hj">0.00</span>
                    </h4>
                            
                </div>
                <div class="span6 pull-right">
                   卡消费:
                        <input type="text" name="vipcard" id="vipcard" class="easyui-numberbox"  precision="1" style="width: 100px;height: 24px" >
                            现金:
                        <input type="text" name="moneys" id="moneys" class="easyui-numberbox"  precision="1"   style="width: 100px;height: 24px" >
                  	<a class="btn  btn-mini btn-primary" onclick="jzthis()">结账</a>
                    <a class="btn  btn-mini btn-danger" onclick="addthis()">挂单保存</a>
                </div>
            </div>
              </form>
            <div class="hr hr8 hr-double hr-dotted"></div>
            <div class="row-fluid">
                <div class="span6">
                    <form class="form-horizontal" id='sptj'>
                        <div class="control-group">
                            <label class="control-label">商品名称:</label>
                            <div class="controls">
                                <input id="wpids" name="wpids" type="text" onchange="getmc()"
                                       style="width: 200px; min-height: 24px;"/> <input id="wpmcs"       dataType="Require"
                                                                                        msg="请选择商品"
                                                                                        name="wpmcs" type="hidden"
                                                                                        style="min-height: 24px;"/>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">商品数量:
                            </label>

                            <div class="controls">
                                <input id="spsls" name="spsls" dataType="Require" msg="请输入商品数量"
                                       onblur="changejg()"
                                       class="easyui-numberbox" type="text" style="min-height: 24px;"/>
                            </div>
                        </div>

                        <div class="control-group">
                            <label class="control-label"> 商品价格:
                            </label>

                            <div class="controls">
                                <input id="spjes" name="spjes" dataType="Require" msg="请输入商品价格"
                                         type="text" style="min-height: 24px;"/>
                            </div>
                        </div>

                        <div class="control-group">
                            <label class="control-label"> 商品提成:
                            </label>

                            <div class="controls">
                                <input id="sptcs" name="sptcs"
                                       type="text" style="min-height: 24px;"/>
                            </div>
                        </div>


                        <div class="control-group"  id="spzkid">
                            <label class="control-label"> 折扣:
                            </label>

                            <div class="controls">
                                <input id="spzks" name="spzks"
                                       type="text" style="min-height: 24px;"/>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label"> 实际价格:
                            </label>

                            <div class="controls">
                                <input id="spsjjgs" name="sjjgs"  dataType="Require" msg="请输入实际价格"
                                       type="text" style="min-height: 24px;"/>
                            </div>
                        </div>
                        <div class="control-group content" style="text-align:center">
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <a class="btn  btn-mini btn-info" onclick="add('sptj')">确定</a>
                        </div>

                    </form>


                </div>
                <!-- /span -->

                <div class="span6">

                    <form class="form-horizontal" id="fwtj">
                        <div class="control-group">
                            <label class="control-label">服务种类:</label>

                            <div class="controls">
                                <input id="fulxid" name="fulxid" onclick="getfwmc();"
                                       type="text" style="min-height: 24px;width: 200px;"/>
                                <input id="fulxs" name="fulxs"
                                       type="hidden" style="min-height: 24px;"/>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">服务价格:
                            </label>

                            <div class="controls">
                                <input id="fujgs" name="fujgs" dataType="Require" msg="请输入服务价格"
                                       onblur="changejgs()"  type="text" style="min-height: 24px;"/>
                            </div>
                        </div>

                        <div class="control-group">
                            <label class="control-label">施工人员:
                            </label>

                            <div class="controls">

                                <select class="populate" multiple   name="grs"  id="grs" style="width:200px" >
                                    <#list user as m>
                                        <option value="${(m.cnname)!}">${(m.cnname)!}</option>
                                    </#list>
                                </select>

                            </div>
                        </div>

                        <div class="control-group">
                            <label class="control-label"> 工人提成:
                            </label>
                            <div class="controls">
                                <input id="grtcs" name="grtcs" dataType="Require" msg="请输入提成"
                                       type="text" style="min-height: 24px;"/>
                            </div>
                        </div>

                        <div class="control-group"  id="fwzkid">
                            <label class="control-label"> 折扣
                            </label>

                            <div class="controls">
                                <input id="fwzks" name="fwzks"
                                       type="text" style="min-height: 24px;"/>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label"> 实际价格:
                            </label>

                            <div class="controls">
                                <input id="fwsjjgs" name="sjjgs"
                                       type="text" style="min-height: 24px;"/>
                            </div>
                        </div>
                        <div class="control-group content" style="text-align:center">
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <a class="btn  btn-mini btn-info" onclick="add('fwtj')">确定</a>

                        </div>
                    </form>


                </div>
                <!-- /span -->
            </div>
            <!-- /row -->
        </div>
    </div>
</div>
</body>
<script src="${ctx}/static/assets/js/chosen.jquery.min.js"></script>

<script>
var wpsptcfs=0; //1按销售比例提成2按件提成
var wpsptcje=0;
var spyxbz=0;
var fwsptcfs=0;//1按销售比例提成2按件提成
var fwsptcje=0;
var fwyxbz=0;
    $('#wpids').select2({
        placeholder: "请输入查询商品名称或者编号",
        minimumInputLength: 0,
        ajax: {
            url: "${ctx}/cgglWp/getWpxx",
            dataType: 'json',
            data: function (term, page) {
                return {
                    term: term,
                    page_limit: 50,
                    page: 0,
                    apikey: "getwpxx"
                };
            },
            results: function (data, page) {
                return { results: data};
            }

        }
    });

    $('#fulxid').select2({
        placeholder: "请输入服务类型",
        minimumInputLength: 0,
        ajax: {
            url: "${ctx}/cgglWp/getFwzl",
            dataType: 'json',
            data: function (term, page) {
                return {
                    term: term,
                    page_limit: 50,
                    page: 0,
                    apikey: "getFwzl"
                };
            },
            results: function (data, page) {
                return { results: data};
            }

        }
    });

    var objn = 1;
    function add(obj) {
        if (obj == 'sptj') {
            if (Validator.Validate(document.getElementById('sptj'))) {
            } else {
                return;
            }
            addrow(objn, obj) ;
            objn =objn+1
            return objn;
        } else if (obj == 'fwtj') {
            if (Validator.Validate(document.getElementById('fwtj'))) {
            } else {
                return;
            }
            addrow(objn, obj) ;
            objn =objn+1
            return objn;
        }
    }

    function addrow(obj, objs) {
        var lrydm = '${(lrydm)!}';
        var lrymc = '${(lrymc)!}';
        var wpmc = $("#wpmcs").val();//商品名称
        var xfid = $("#wpids").val();//物品id
        var spsl = $("#spsls").val();//商品数量
        var spje = $("#spjes").val();//商品金额
        var sptc =0;//商品提成
        var spzk = $('#spzks').combotree('getValue');//商品折扣
        var spsjjg = $("#spsjjgs").val();//商品实际金额
        if (wpsptcfs==1){
        	sptc=spsjjg*wpsptcje/100;
        }else if (wpsptcfs==2){
        	sptc=spsl*wpsptcje;
        } else {
        	sptc=0;
        }
        var fulx = $("#fulxs").val();//服务类型
        var  fulxid=$("#fulxid").val();
        var fujg = $("#fujgs").val();//服务价格
        var gr = $("#grs").val();//工人
        var grtc = $("#grtcs").val();//工人提成
        var fuzk =  $('#fwzks').combotree('getValue');//服务折扣
        var fwsjjg = $("#fwsjjgs").val();//服务实际价格
        if (fwsptcfs==1){
        	grtc=fwsjjg*fwsptcje/100;
        }else if (fwsptcfs==2){
        	grtc=1*fwsptcje;
        } else {
        	grtc=0;
        }
        var   wpids=  $("#wpids").val();
        if (objs == 'sptj') {
            var newRow = '<tr  id=' + "tr" + obj + ' class="info">' +
                    '<input type="hidden"   name="wpids"  id='+"wpids" + obj + ' value="sptj"   />' +
                    '<input type="hidden"   name="xfbz"  id='+"xfbz" + obj + ' value="sptj"   />' +

                    '<td style="white-space:nowrap;"  >' + wpmc + ' <input type="hidden"   name="xfid" id=' + "xfid" + obj + '   /><input type="hidden"   name="xfmc" id=' + "xfmc" + obj + '   /></td>' +
                    '<td style="white-space:nowrap;"  >' + spsl + ' <input type="hidden"   name="xfsl" id=' + "xfsl" + obj + '   /></td>' +
                    '<td style="white-space:nowrap;"  >' + spje + ' <input type="hidden"   name="xfje" id=' + "xfje" + obj + '   /></td>' +
                    '<td style="white-space:nowrap;"  >' + spzk + ' <input type="hidden"   name="xfzk" id=' + "xfzk" + obj + '   /></td>' +
                    '<td style="white-space:nowrap;"  >' + spsjjg + ' <input type="hidden"   name="xfsjjg" id=' + "xfsjjg" + obj + '   /></td>' +
                    '<td style="white-space:nowrap;"  >' + sptc + ' <input type="hidden"   name="xftc" id=' + "xftc" + obj + '   /></td>' +
                    '<td style="white-space:nowrap;"  >' + lrymc + ' <input type="hidden"   name="czymc" id=' + "czymc" + obj + '   />' +
                    '<a  class="btn btn-mini btn-success"  onclick="editRows('+obj+')">修改</a>' +
                    '<a  class="btn btn-mini btn-danger"    onclick="deleteRows(this,'+obj+')"> 删除</a></td>' +
                    '</tr>';

            $("#tabnew tr:last").after(newRow);
            $("#xfmc" + obj + "").attr("value", wpmc);
            $("#xfsl" + obj + "").attr("value", spsl);
            $("#xfje" + obj + "").attr("value", spje);
            $("#czymc" + obj + "").attr("value", lrymc);
            $("#xftc" + obj + "").attr("value", sptc);
            $("#xfzk" + obj + "").attr("value", spzk);
            $("#xfsjjg" + obj + "").attr("value", spsjjg);
            $("#wpids" + obj + "").attr("value", wpids);
            $("#xfid" + obj + "").attr("value", xfid);

            var ljsl=$("#ljsl").html();
            var hj=$("#hj").html();
            var lj= parseFloat(ljsl)+ parseFloat(spsl);
            var hjs=   parseFloat(hj)+ parseFloat(spsjjg) ;
            $("#ljsl").html(lj) ;
            $("#hj").html(toDecimal(hjs));
         
            $("#wpmcs").attr("value","");
            $("#spsls").attr("value","");
            $("#spjes").attr("value","");
            $("#sptcs").attr("value","");
          //  $("#spzks").attr("value","");
            $('#spzks').combotree('setValue', "")
            $("#spsjjgs").attr("value","");
            $("#wpids").select2("val", "");
        }
        if (objs == 'fwtj') {
            var newRow = '<tr  id=' + "tr" + obj + ' class="info">' +
                    '<input type="hidden"   name="fulxid" id=' + "fulxid" + obj + '   />' +
                    '<input type="hidden"   name="xfbz" id=' + "xfbz" + obj + ' value="fwtj"   />' +
                    '<td style="white-space:nowrap;"  >' + fulx + '  <input type="hidden"   name="xfid" id=' + "xfid" + obj + '   /><input type="hidden"   name="xfmc" id=' + "xfmc" + obj + '   /></td>' +
                    '<td style="white-space:nowrap;"  >1 <input type="hidden"   name="xfsl" id='+"xfsl" + obj + ' value="1"/></td>' +
                    '<td style="white-space:nowrap;"  >' + fujg + ' <input type="hidden"   name="xfje" id=' + "xfje" + obj + '   /></td>' +
                    '<td style="white-space:nowrap;"  >' + fuzk + ' <input type="hidden"   name="xfzk" id=' + "xfzk" + obj + '   /></td>' +
                    '<td style="white-space:nowrap;"  >' + fwsjjg + ' <input type="hidden"   name="xfsjjg" id=' + "xfsjjg" + obj + '   /></td>' +
                    '<td style="white-space:nowrap;"  >' + grtc + ' <input type="hidden"   name="xftc" id=' + "xftc" + obj + '   /></td>' +
                    '<td style="white-space:nowrap;"  >' + gr + ' <input type="hidden"   name="czymc" id=' +"czymc" + obj + '   />' +
                    '<a  class="btn btn-mini btn-success"  onclick="editRows('+obj+')">修改</a>' +
                    '<a  class="btn btn-mini btn-danger"    onclick="deleteRows(this,'+obj+')"> 删除</a></td>' +
                    '</tr>';
            $("#tabnew tr:last").after(newRow);
            $("#xfmc" + obj + "").attr("value", fulx);
            $("#xfid" + obj + "").attr("value", fulxid);//服务类型id

            $("#xfsl" + obj + "").attr("value", "1");
            $("#xfje" + obj + "").attr("value", fujg);
            $("#czymc" + obj + "").attr("value", gr);
            $("#xftc" + obj + "").attr("value", grtc);
            $("#xfzk" + obj + "").attr("value", fuzk);
            $("#xfsjjg" + obj + "").attr("value", fwsjjg);
            var ljsl=$("#ljsl").html();
            var hj=$("#hj").html();
            var lj= parseFloat(ljsl)+ parseFloat(1);
            var hjs=   parseFloat(hj)+ parseFloat(fwsjjg);
            $("#ljsl").html(lj) ;
            $("#hj").html(toDecimal(hjs));
            $("#fulxs").attr("value","");
            $("#fujgs").attr("value","");
            $("#grtcs").attr("value","");
            $('#fwzks').combotree('setValue', "")
            $("#fwsjjgs").attr("value","");
            $("#fulxid").select2("val", "");
            $("#grs").select2("val", "");
        }

    }
    function editRows(obj){
        var xfmc=  $("#xfmc"+obj+"").val() ;
        var xfsl=  $("#xfsl"+obj+"").val() ;
        var xfjg=  $("#xfje"+obj+"").val() ;
        var xfzk=  $("#xfzk"+obj+"").val() ;
        var xfsjjg=  $("xfsjjg"+obj+"").val() ;
        var xftc=  $("#xftc"+obj+"").val() ;  //提成
        var czymc=  $("#czymc"+obj+"").val() ;  //人员
        var xfbz=$("#xfbz"+obj+"").val() ;
        if(xfbz =="sptj"){
           $("#wpmcs").attr("value",xfmc);
            $("#wpids").select2("data", {id: "", text: xfmc});
            $("#spsls").attr("value",xfsl);
            $("#spjes").attr("value",xfjg);
            $("#sptcs").attr("value",xftc);
            $("#spzks").combotree('setValue',xfzk);
            $("#spsjjgs").attr("xfsjjg",xfsjjg);
        }    else {
            $("#fulxs").attr("value",xfmc);
            $("#fulxid").select2("data", {id: "", text: xfmc});
            $("#fujgs").attr("value",xfjg);
            $("#grtcs").attr("value",xftc);
            $("#fwzks").combotree('setValue',xfzk);
            $("#fwsjjgs").attr("xfsjjg",xfsjjg);
            var str=  czymc.split(",")
            var datas="";
            for(var i=0;i<str.length;i++){
				            datas+=""+str[i]+",";
            }
            var datas="[" +datas.substr(0,datas.length-1)+"]";
            var json = eval('(' + datas + ')');
          //  $("#grs").select2("data",json);
			     $("#grs").select2("val",json);

        }
        deleteRows("",obj);
    }
    function deleteRows(objs,obj){

        var ljsl=$("#ljsl").html();
        var hj=$("#hj").html();
        var sjje=""; //实际金额
        var sl=0;
        var xfbz=$("#xfbz"+obj+"").val();
        if(xfbz =="sptj"){
            sjje =$("#xfsjjg"+obj+"").val() ;
            sl =$("#xfsl"+obj+"").val() ;
        }    else {
            sjje =$("#xfsjjg"+obj+"").val() ;
            sl=1;
        }
        var lj= parseFloat(ljsl)- parseFloat(sl);
        var hjs=   parseFloat(hj)- parseFloat(sjje);
       $("#ljsl").html(lj);
       $("#hj").html(toDecimal(hjs));
       $("#tr"+obj+"").remove();

    }

    function getmc(){
        var data = $("#wpids").select2("data");
        var objs=JSONstringify(data);
        var arr = eval('(' + objs + ')');
        document.getElementById("wpmcs").value=(arr['text']==undefined ) ? "" : arr['text'];
        document.getElementById("spjes").value=(arr['ckdj']==undefined ) ? "" : arr['ckdj'];
        document.getElementById("sptcs").value=(arr['sptc']==undefined ) ? "" : arr['sptc'];
        wpsptcfs= (arr['sptcfs']==undefined ) ? "0" : arr['sptcfs'];
        wpsptcje=  (arr['sptc']==undefined ) ? "0": arr['sptc'];
        spyxbz=  (arr['yxbz']==undefined ) ? "0" : arr['yxbz'];
		if (spyxbz=="2") {
			$("#spzkid").show();  
		} else {
			$("#spzkid").hide();  
		}    
    }
    function getfwmc(){
        var data = $("#fulxid").select2("data");
        var objs=JSONstringify(data);
        var arr = eval('(' + objs + ')');
        document.getElementById("fulxs").value=(arr['text']==undefined ) ? "" : arr['text'];
        document.getElementById("fujgs").value=(arr['fwjg']==undefined ) ? "" : arr['fwjg'];
        document.getElementById("grtcs").value=(arr['grtc']==undefined ) ? "" : arr['grtc'];
        document.getElementById("fwsjjgs").value=(arr['fwjg']==undefined ) ? "" : arr['fwjg'];
        fwsptcfs= (arr['sptcfs']==undefined ) ? "0" : arr['sptcfs'];
        fwsptcje=  (arr['grtc']==undefined ) ? "0": arr['grtc'];
        fwyxbz=  (arr['yxbz']==undefined ) ? "0" : arr['yxbz'];
		if (fwyxbz=="2") {
			$("#fwzkid").show();  
		} else {
			$("#fwzkid").hide();  
		}
 
    }


    //转json
    function JSONstringify(Json){
        if($.browser.msie){
            if($.browser.version=="7.0"||$.browser.version=="6.0"){
                var  result=jQuery.parseJSON(Json);
            }else{
                var result=JSON.stringify(Json);
            }
        }else{
            var result=JSON.stringify(Json);
        }
        return result;
    }
    function changejg(){
        var spsl = $("#spsls").val();//商品数量
        if (spsl ==""){
            return;
        }
        var spje = $("#spjes").val();//商品金额
        var spzk =   $('#spzks').combotree('getValue');//商品折扣
        var spsjjg="";
        if (spzk =="" ||spzk =="0"){
            spsjjg= spsl*spje;
        }else {
            spsjjg= spsl*spje*spzk/10;
        }
        $("#spsjjgs").attr("value",spsjjg);//商品实际金额
    }
    function changejgs(){
        var spsl = 1;//商品数量
        var fwsjjg="";
        if (spsl ==""){
            return;
        }
        var spje = $("#fujgs").val();//商品金额
        var spzk =   $('#fwzks').combotree('getValue');//商品折扣
        if (spzk =="" || spzk=="0"){
            fwsjjg= spsl*spje;
        } else {
            fwsjjg= spsl*spje*spzk/10;
        }
        $("#fwsjjgs").attr("value",fwsjjg);//商品实际金额

    }


    jQuery(function($) {
        $("#grs").select2({
        });

    });
    function toDecimal(x) {
        var f = parseFloat(x);
        if (isNaN(f)) {
            return false;
        }
        var f = Math.round(x*100)/100;
        var s = f.toString();
        var rs = s.indexOf('.');
        if (rs < 0) {
            rs = s.length;
            s += '.';
        }
        while (s.length <= rs + 2) {
            s += '0';
        }
        return s;
    }

    $('#fwzks').combotree({
        url:"${ctx}/common/comboTree/type='zkgl'?table=codes&fields=code as id,name as text&pid=pid&pidStart=0&id=id"  ,
        onSelect: function (node) {
            var spje = $("#fujgs").val();//商品金额
            var spzk = node.id;//商品折扣
            var spsl = 1;//商品数量
            var fwsjjg="";
            if (spzk =="" ||spzk=="0"){
                fwsjjg= spsl*spje;
            } else {
                fwsjjg= spsl*spje*spzk/10;
            }
            $("#fwsjjgs").attr("value",fwsjjg);//商品实际金额
        }
    })
    $('#spzks').combotree({
        url:"${ctx}/common/comboTree/type='zkgl'?table=codes&fields=code as id,name as text&pid=pid&pidStart=0&id=id" ,
        onSelect: function (node) {
            var spsl = $("#spsls").val();//商品数量
            var spje = $("#spjes").val();//商品金额
            var spzk = node.id;//商品折扣
            if (spsl ==""){
                return;
            }
            if (spzk =="" ||spzk=="0"){
                spsjjg= spsl*spje;
            }   else {
                spsjjg= spsl*spje*spzk/10;
            }
            $("#spsjjgs").attr("value",spsjjg);//商品实际金额
        }
    });

</script>
</@layout>
